package com.daiqee.shiro.realm;

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import com.daiqee.pojo.Department;
import com.daiqee.pojo.Role;
import com.daiqee.pojo.User;
import com.daiqee.service.impl.DepartmentService;
import com.daiqee.service.impl.LoginService;
import com.daiqee.service.impl.RoleService;

/**
 * 账号登陆域
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:58:26
 * @package com.daiqee.realm
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
public class UserLoginRealm extends AuthorizingRealm {

	@Autowired
	private LoginService loginService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private DepartmentService departmentService;
	
	/**
	 * 登陆认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
			//吧接收过来的令牌进行转换，方便操作！
			UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
			//获取用户名
		 	String username= token.getUsername();
		 	//获取密码
		 	String password= new String(token.getPassword());
		 	//创建一个登陆用户
		 	User tempUser = new User();
		 	//给该对象设置一个账号名
		 	tempUser.setUserName(username);
		 	//给该对象设置一个密码
		 	tempUser.setUserPassword(password);
		 	//进行数据库校验
		 	User user = loginService.loginUser(tempUser);
	        if(user==null){ //判断该对象是否存在
	        	//没有查询到该对象抛出异常
	            throw new AuthenticationException(); 
	        }
	        //获取当前用户
	        Subject subject = SecurityUtils.getSubject();
	        //存储当前用户的相关信息
	        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user,
	        		user.getUserPassword(), this.getName());
	        //吧当前用户存储到session域中
	        subject.getSession().setAttribute("user", user);
	        return simpleAuthenticationInfo; 
	}
	
	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
			//获取你刚刚登陆的用户的用户名  
			User user = (User) principals.getPrimaryPrincipal();  
		    
			//更具账号查询角色
		    Role role = roleService.selectById(user.getUserRole());
		    
		    //更具账号查询部门
		    Department department = departmentService.selectById(user.getUserDepartment());
		    
		    //将权限信息封闭为AuthorizationInfo  
		    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
		    
		    //吧查询到的角色对象添加到simpleAuthorizationInfo中
		    simpleAuthorizationInfo.addRole(role.getRoleName());
		    
		    //吧查询到的权限添加到simpleAuthorizationInfo中
		    simpleAuthorizationInfo.addStringPermission(department.getDeptName() + ":" + role.getRoleName());
		    
		    return simpleAuthorizationInfo;  
	}
}
